<?php


class Func
{
  var $name;
  var $value;

  public function __construct($name)
  {
    $this->name = $name;
  }

  public function sql($exp = "")
  {
    if ($exp)
      {
	return "{$this->name}({$exp})";
      }
    else if ($this->value)
      {	
	return "{$this->name}({$this->value})";
      }
    return "{$this->name}()";
  }

  public function __toString()
  {
    return $this->sql();
  }

  public static function length()
  {
    $f = new Func("LENGTH");
    return $f;
  }

  public static function lower()
  {
    $f = new Func("LOWER");
    return $f;
  }

  public static function upper()
  {
    $f = new Func("UPPER");
    return $f;
  }

  public static function not()
  {
    $f = new Func("NOT");
    return $f;
  }

  public static function uuid()
  {
    $f = new Func("UUID");
    return $f;
  }


  public static function now()
  {
    $f = new Func("NOW");
    return $f;
  }

  public static function addtime($list)
  {

    $list = func_get_args();
    if (count($list) == 0)
      {
	return ;
      }
    $sql = "";
    $sep = "";
    
    foreach ($list as $a)
      {
	if ($a instanceof Attribute)
	  {
	    $sql .= $sep . $a->sql();
	  }
	else if ($a instanceof Func)
	  {
	    $sql .= $sep . $a->sql();
	  }
	else
	  {	    
	    $sql .= $sep . $a;
	  }
	$sep = ", ";
      }

    $f = new Func("ADDTIME");
    $f->value = $sql;
    return $f;
  }

  public static function count($a = '*')
  {
    $f = new Func("COUNT");
    if ($a instanceof Attribute )
      {
	$f->value = $a->sql();
      }
    else
      {
	$f->value = $a;
      }
    return $f;
  }

  public static function concat($list)
  {

    $list = func_get_args();
    if (count($list) == 0)
      {
	return ;
      }
    $sql = "";
    $sep = "";
    
    foreach ($list as $a)
      {
	if ($a instanceof Attribute)
	  {
	    $sql .= $sep . $a->sql();
	  }
	else
	  {	    
	    $sql .= $sep . $a;
	  }
	$sep = ", ";
      }

    $f = new Func("CONCAT");
    $f->value = $sql;
    return $f;
  }


  public static function add($list)
  {

    $list = func_get_args();
    if (count($list) == 0)
      {
	return ;
      }
    $sql = "";
    $sep = "";
    
    foreach ($list as $a)
      {
	if ($a instanceof Attribute)
	  {
	    $sql .= $sep . $a->sql();
	  }
	else
	  {	    
	    $sql .= $sep . $a;
	  }
	$sep = " + ";
      }

    $f = new Func("");
    $f->value = $sql;
    return $f;
  }

  public static function oid()
  {
    $con = PDOx::get();
    $res = $con->fetch("SELECT UUID() AS uuid;");
    return $res[0]["uuid"];
  }
}

?>